﻿using AviewLightSource;
using FinalTestMachine.Connect;
using FinalTestMachine.Motion.IO;
using FinalTestMachine.Motion.Product;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UdpWpfDemo3;

namespace FinalTestMachine.Motion.Process
{
    /// <summary>
    /// 工位四测试
    /// </summary>
    public class FourTestStation : Process_TestStationBase
    {
        public static readonly FourTestStation Cur = new FourTestStation();

        private FourTestStation() : base(SiteConnectManager.Cur.GetCommunicationHelper(4), "工位四", 3)
        {
        }

        public static FourTestStation CreateInstance()
        {
            return Cur;
        }

        //给测试发数据
        public string AllInfo()
        {
            string tempStr = string.Empty;

            string temStr_StartTime = string.Empty;
            string temStr_EndTime = string.Empty;

            for(int i = 1; i <= GlobalParams.WorkstationSum; i++)
            {
                string fieldName = $"Site{i}IsUse";
                if (GlobalParams.GetFieldValue<bool>(Funs.FunStationSettings.Cur, fieldName))
                {
                    temStr_StartTime = CurrentProduct.GetTestStation(i).StartTime;
                    break;
                }
            }

            tempStr += $"Name=startTestTime,value={temStr_StartTime},LowLimit=-,UpLimit=-,result=PASS;";

            WriteToUser($"测试开始时间：{temStr_StartTime}；测试结束时间：{CurrentProduct.GetTestStation(6).EndTime}");

            double timeSpan = 0;
            tempStr += $"Name=EndTime,value={GetTimeNow()},LowLimit=-,UpLimit=-,result=PASS;";

            for (int i = 1; i <= GlobalParams.WorkstationSum; i++)
            {
                timeSpan += CurrentProduct.GetTestStation(i).TotalTime;
                foreach (var item in CurrentProduct.GetTestStation(i).StationTestItems)
                {
                    tempStr += $"Name={item.name},Value={item.value},LowLimit={item.lowLimit},UpLimit={item.upLimit},result={item.result};";
                }
            }

            return tempStr;
        }

        public string GetResult()
        {
            WriteToUser("开始获取发送数据");
            while (!isCanGetResult)
            {
                if (!IsProcessRunning)
                {
                    break;
                }

                Sleep(10);
            }
            WriteToUser("允许获取发送数据");

            string tempStr = string.Empty;

            tempStr = "PASS";

            for(int i = 1; i <= GlobalParams.WorkstationSum; i++)
            {
                foreach (var item in CurrentProduct.GetTestStation(i).StationTestItems)
                {
                    if (item.result.ToUpper().Equals("FAIL"))
                    {
                        tempStr = "FAIL";
                    }
                }
            }
            return tempStr;
        }

        private bool isCanGetResult { set; get; }

        public override bool AxiOrCylinderToWorkPos()
        {
            Aixs.Site4.Aix_Site4_RelayMirror.Cur.WorkPos.Mov();
            StaticIOHelper.Cylinder_Site4LeftLightUpDown.WorkPos();
            StaticIOHelper.Cylinder_Site4RightLightUpDown.WorkPos();
            Aixs.Site4.Aix_Site4_PlaneSourceLift.Cur.WorkPos.Mov();
            return true;
        }

        public override bool AxiOrCylinderToWaitPos()
        {
            Aixs.Site4.Aix_Site4_PlaneSourceLift.Cur.WaitPos.Mov();
            StaticIOHelper.Cylinder_Site4LeftLightUpDown.OriginPos();
            StaticIOHelper.Cylinder_Site4RightLightUpDown.OriginPos();
            Aixs.Site4.Aix_Site4_RelayMirror.Cur.WaitPos.Mov();
            return true;
        }

        public override bool OtherActionBeforeExit()
        {
            return true;
        }

        public override ProductTestStation GetCurrentStation()
        {
            return CurrentProduct?.GetTestStation(4);
        }

        public override ProductTestStation GetLastStation()
        {
            return CurrentProduct?.GetTestStation(3);
        }

    }
}